home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
HyperLib 1997 Winter - Disc 1
/
HYPERLIB-1997-Winter-CD1.ISO.7z
/
HYPERLIB-1997-Winter-CD1.ISO
/
オンラインウェア
/
BUS
/
TMCM Software and Labs.sit
/
Software for TMCM 7_95
/
Files for Lab 6
/
PrimeNumbers
< prev
Wrap
Text File
|
1995-07-10
|
6KB
|
149 lines
; This file contains a subroutine, "remainder", for computing
; remainders. The subroutine is at the end of the file. You'll
; find a description of the subroutine there.
; Your assignment is to write a main program that will generate
; a list of prime numbers. You can just follow the algorithm
; that is outlined in the comments. All the labeled data
; locations that you will need are already declared,
; and some of the program is already written. Read the
; comments carefully. You only have to add about 16 instructions
; to the program, and 7 of those are just for calling the
; subroutine.
; Once the program is written, you can let the program run at
; full speed and watch the primes being added to the list
; starting at location 50. It is also interesting to watch
; the program at full speed in Graphics Display mode.
; -------------------- MAIN PROGRAM ---------------------------------
LOD-C 2 ; 2 is the first number that will be tested.
STO p ; Memory location "p" holds the number that
; is currently begin tested.
LOD-C 0 ; Make sure there is a zero to mark
STO prime_list ; the end of the list of primes.
LOD-C prime_list ; "end_of_list" points to the first
STO end_of_list ; available location in the list of primes.
loop: ; The main loop of the program starts here.
; In this loop, the number "p" is tested for
; primality. If it is prime, p is added to
; the list of prime numbers; if not, it is
; simply discarded.
; First, set "ptr" to point to the start
; of the list of primes.
get_d: ; Get the number, d, in the address indicated
; by ptr. (Use indirect addressing!)
; If the number d is zero, then p is prime, so
; jump to location "add_to_list" (to add p
; to the list of primes and then go on to the
; next value of p).
; In the case d is not zero, check whether
; the remainder is zero when p is divided by d,
; using the subroutine. If so, then p is NOT
; prime. Go on to the next value of p.
; In the case where the remainder is NOT zero,
; add 1 to ptr and jump back to "get_d" to
; test the next number in the list.
; (The rest of the program is already written.)
add_to_list: ; Add p to the list of prime numbers
LOD p
STO-I end_of_list
LOD end_of_list
INC
STO end_of_list
LOD-C 0 ; (Make sure there is a zero at the end of the list.)
STO-I end_of_list
next_p: LOD p ; Add 1 to p (the number being tested)
INC
STO p
JMP loop ; and go back to the start of the main loop.
; (This is an infinite loop that will never end!)
; Finally, here are the data locations needed
; in the maim program. (The main program
; will also need to refer to the location
; "prime_list", which is the starting location
; for the list of primes. And when it calles
; the subroutine, it will need to use the
; memory locations "ret_adr", "n", "d",
; and possibly "rem".)
p: data ; The number begin tested for primality.
ptr: data ; A pointer to one of the primes already
; in the list of primes. (That is, the number
; stored in ptr is one of the numbers in the list.)
end_of_list: data ; A pointer to the location that follows the
; end of the list of primes. When a new prime
; is found, it is placed in this location. (You
; won't need to worry about this in the part of
; the program you are writing.)
@50 ; The list of prime numbers will start at location 50
prime_list: data
; -------------------------- Subroutine "remainder" ----------------
; This subroutine computes the remainder when one postive number, n,
; is divided by another positibe number, d. To use it:
; (1) store the number n in memory location "n"
; (2) store the number d in memory location "d"
; (3) store the return address in location "ret_adr"
; (4) jump to location "remainder"
; When the subroutine terminates, the remainder will be in memory
; location "rem." It will ALSO be in the accumulator (AC).
@900 ; subroutine loads starting at location 900
ret_adr: data ; return address
n: data ; parameters, as described above
d: data
rem: data
remainder: LOD d ; Starting point for subroutine.
STO temp_r ; (You don't need to understand anything
; from here on.)
L1r: LOD temp_r
SHL
STO temp_r
SUB n
JMN L1r
JMZ L1r
L2r: LOD temp_r
SHR
STO temp_r
SUB d
JMN done_r
LOD n
SUB temp_r
JMN L2r
STO n
JMP L2r
done_r: LOD n
STO rem
JMP-I ret_adr
temp_r: data